package PL.prac1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Scanner;


public class Invitacion {

	private File salida;
	private String entrada;
	private String front;
	private String body;
	private String back;
	private BufferedWriter out;
	private campos c;
	
	public Invitacion (String fichin) throws IOException{
		entrada = fichin;
		salida = new File("invcss.html");
		if (salida.exists()) {
			salida.delete();
			salida.createNewFile();
		}
		c = new campos();
		out = new BufferedWriter(new FileWriter(salida));
		front = body = back = null;
	}
	
	public String Getfront() {
		return front;
	}
	
	public String GetBody() {
		return body;
	}
	
	public String Getback() {
		return back;
	}
	public void Read () throws IOException {
		Scanner reader = new Scanner (entrada);
		reader.useDelimiter("<front>");
		reader.next();
		reader.useDelimiter("</front>");
		front = reader.next();
		reader.useDelimiter("<body>");
		reader.next();
		reader.useDelimiter("</body>");
		body = reader.next();
		reader.useDelimiter("<back>");
		reader.next();
		reader.useDelimiter("</back>");
		back = reader.next();
	}
	
	public void Write() throws IOException {
		front = c.Translatefront(front);
		out.write(front);
		body = c.TranslateBody(body);
		out.write(body);
		back = c.TranstaleBack(back);
		out.write(back);
	}
	
	public void Convert() throws IOException {
		Read();
		out.write("<HTML>\n<HEAD>\n");
		out.write("<TITLE> Invitation (sgmlpl/CSS formatting) </TITLE>\n");
		out.write("<LINK href=\"invit.css\" rel=\"stylesheet\" type=\"text/css\">\n");
		out.write("<!-- 24 August 1998 mg -->\n");
		out.write("</HEAD>\n<BODY>\n");
		out.write("<H1><font color=\"blue\">INVITATION</font></H1>\n");
		Write();
		out.write("</BODY>\n</HTML>\n");
		out.close();
	}
	
	public static void main(String[] args) throws IOException {
		File fichin = new File("invitation.xml");
		String linea;
		StringWriter aux = new StringWriter();
		FileReader fr = new FileReader (fichin);
		BufferedReader br = new BufferedReader(fr);
		while((linea = br.readLine())!=null)
			aux.write(linea+"\n");
		Invitacion i = new Invitacion (aux.toString());
		br.close();
		fr.close();
		i.Convert();
	}

}
